﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Configuration;

/// <summary>
/// Summary description for BaseClass
/// </summary>
/// 

using System.Web.Security;
using Assesment.Web.Support;

namespace Assesment.Web
{
    public class BasePage : Page, IUserProfile
    {
        public BasePage()
        {
            this.Init += new EventHandler(BasePage_Init);

        }

        private void BasePage_Init(object sender, EventArgs e)
        {
            Response.CacheControl = "no-cache";
            Response.AddHeader("Pragma", "no-cache");
            Response.Expires = -1;

            MenuID = (Request.Params["menuid"] == null ? 0 : Convert.ToInt32(Request.Params["menuid"]));

            UserLoggedIn = "Anonymous";
            //SubSectorName = string.Empty;

            if (HttpContext.Current.User != null)
            {
                if (HttpContext.Current.User.Identity.IsAuthenticated)
                {
                    UserLoggedIn = this.Context.User.Identity.Name;

                    if (HttpContext.Current.User.Identity is FormsIdentity)
                    {
                        FormsIdentity id = HttpContext.Current.User.Identity as FormsIdentity;
                        FormsAuthenticationTicket ticket = id.Ticket;

                        string userData = ticket.UserData.Trim();
                        if (!string.IsNullOrEmpty(userData))
                        {
                            string[] datas = userData.Split('|');

                            UserID = int.Parse(datas[0]);
                            if (!string.IsNullOrEmpty(datas[1]))
                                CabangID = int.Parse(datas[1]);

                            //if (!string.IsNullOrEmpty(datas[1]))
                            //    PositionID =Convert.ToInt32(datas[1]);

                            //if (!string.IsNullOrEmpty(datas[2]))
                            //    DepartmentID = int.Parse(datas[2]);

                        }
                    }
                }
            }

        }

        public int MenuID
        {
            get;
            set;
        }

        public string UserLoggedIn
        {
            get;
            set;
        }

        public int UserID
        {
            get;
            set;
        }

        public int CabangID
        {
            get;
            set;
        }

        //public string SubSubSectorIDs
        //{
        //    get;
        //    set;
        //}

        //public int? DebtorID
        //{
        //    get;
        //    set;
        //}

        //public int RegistrationID
        //{
        //    get;
        //    set;
        //}

        //public int? UserTypeID
        //{
        //    get;
        //    set;
        //}

        //public string SubSectorName
        //{
        //    get;
        //    set;
        //}

        //public bool IsIndividual
        //{
        //    get;
        //    set;
        //}

        //public int? PositionID
        //{
        //    get;
        //    set;
        //}

        //public int? DepartmentID
        //{
        //    get;
        //    set;
        //}

    }

    public abstract class BaseHandler : IHttpHandler, IUserProfile
    {

        public virtual bool IsReusable
        {
            get { return false; }
        }

        public virtual void ProcessRequest(HttpContext context)
        {
            UserLoggedIn = "Anonymous";
            //SubSectorName = string.Empty;

            if (HttpContext.Current.User != null)
            {
                if (HttpContext.Current.User.Identity.IsAuthenticated)
                {
                    UserLoggedIn = context.User.Identity.Name;

                    if (HttpContext.Current.User.Identity is FormsIdentity)
                    {
                        FormsIdentity id = HttpContext.Current.User.Identity as FormsIdentity;
                        FormsAuthenticationTicket ticket = id.Ticket;

                        string userData = ticket.UserData.Trim();
                        if (!string.IsNullOrEmpty(userData))
                        {
                            string[] datas = userData.Split('|');

                            UserID = int.Parse(datas[0]);
                            if (!string.IsNullOrEmpty(datas[1]))
                                CabangID = int.Parse(datas[1]);

                            //if (!string.IsNullOrEmpty(datas[1]))
                            //    PositionID = Convert.ToInt32(datas[1]);

                            //if (!string.IsNullOrEmpty(datas[2]))
                            //    DepartmentID = int.Parse(datas[2]);


                        }
                    }
                }
            }

        }

        public string UserLoggedIn
        {
            get;
            set;
        }

        public int UserID
        {
            get;
            set;
        }

        public int CabangID
        {
            get;
            set;
        }

        //public string SubSubSectorIDs
        //{
        //    get;
        //    set;
        //}

        //public int? DebtorID
        //{
        //    get;
        //    set;
        //}

        //public int RegistrationID
        //{
        //    get;
        //    set;
        //}

        //public int? UserTypeID
        //{
        //    get;
        //    set;
        //}

        //public string SubSectorName
        //{
        //    get;
        //    set;
        //}
        //public bool IsIndividual
        //{
        //    get;
        //    set;
        //}

        //public int? PositionID
        //{
        //    get;
        //    set;
        //}

        //public int? DepartmentID
        //{
        //    get;
        //    set;
        //}
    }

    public class BaseMasterPage : MasterPage, IUserProfile
    {

        public BaseMasterPage()
        {
            this.Init += new EventHandler(BaseMasterPage_Init);
        }

        protected void BaseMasterPage_Init(object sender, EventArgs e)
        {
            Response.CacheControl = "no-cache";
            Response.AddHeader("Pragma", "no-cache");
            Response.Expires = -1;

            MenuID = (Request.Params["menuid"] == null ? 0 : Convert.ToInt32(Request.Params["menuid"]));

            UserLoggedIn = "Anonymous";
            //SubSectorName = string.Empty;

            if (HttpContext.Current.User != null)
            {
                if (HttpContext.Current.User.Identity.IsAuthenticated)
                {
                    UserLoggedIn = this.Context.User.Identity.Name;

                    if (HttpContext.Current.User.Identity is FormsIdentity)
                    {
                        FormsIdentity id = HttpContext.Current.User.Identity as FormsIdentity;
                        FormsAuthenticationTicket ticket = id.Ticket;

                        string userData = ticket.UserData.Trim();
                        if (!string.IsNullOrEmpty(userData))
                        {
                            string[] datas = userData.Split('|');

                            UserID = int.Parse(datas[0]);
                            if (!string.IsNullOrEmpty(datas[1]))
                                CabangID = int.Parse(datas[1]);
                            //if (!string.IsNullOrEmpty(datas[1]))
                            //    SubSubSectorIDs = datas[1];

                            //if (!string.IsNullOrEmpty(datas[2]))
                            //    DebtorID = int.Parse(datas[2]);

                            //if (!string.IsNullOrEmpty(datas[3]))
                            //    RegistrationID = int.Parse(datas[3]);

                            //if (!string.IsNullOrEmpty(datas[4]))
                            //    UserTypeID = int.Parse(datas[4]);

                            //if (!string.IsNullOrEmpty(datas[5]))
                            //    SubSectorName = datas[5];
                            //if (!string.IsNullOrEmpty(datas[6]))
                            //    IsIndividual = bool.Parse(datas[6]);
                        }
                    }
                }
            }

        }

        public int MenuID
        {
            get;
            set;
        }

        public string UserLoggedIn
        {
            get;
            set;
        }

        public int UserID
        {
            get;
            set;
        }

        public int CabangID
        {
            get;
            set;
        }

        ////public int? DebtorID
        ////{
        ////    get;
        ////    set;
        ////}

        ////public string SubSubSectorIDs
        ////{
        ////    get;
        ////    set;
        ////}

        ////public int RegistrationID
        ////{
        ////    get;
        ////    set;
        ////}

        ////public int? UserTypeID
        ////{
        ////    get;
        ////    set;
        ////}

        ////public string SubSectorName
        ////{
        ////    get;
        ////    set;
        ////}
        ////public bool IsIndividual
        ////{
        ////    get;
        ////    set;
        ////}

        ////public int? PositionID
        ////{
        ////    get;
        ////    set;
        ////}

        ////public int? DepartmentID
        ////{
        ////    get;
        ////    set;
        ////}
    }

}